<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 显示树形编辑菜单
 * @param  arr  $data  要处理的数据
 * @param  int  $Id    父级id
 * @param  INT $level 层级
 * @return html         返回html
 */
function getTree($data, $Id, $level = 0)
{
    $html = '';
    foreach ($data as $k => $v) {
        if ($v['pid'] == $Id) {
            //父亲找到儿子
            if (isset($v['checked']) && $v['checked'] == 'y') {
                //设置选中状态
                $html .= "<li class=\"auth-t" . $level . "\" data-level=\"" . $level . "\"><input type=\"checkbox\" name=\"rules[]\" lay-skin=\"primary\" title=\"" . $v['title'] . "\" value=\"" . $v['id'] . "\" checked=\"\" lay-checkbox lay-filter=\"formauth\">";
            } else {
                $html .= "<li class=\"auth-t" . $level . "\" data-level=\"" . $level . "\"><input type=\"checkbox\" name=\"rules[]\" lay-skin=\"primary\" title=\"" . $v['title'] . "\" value=\"" . $v['id'] . "\" lay-checkbox lay-filter=\"formauth\" >";
            }
            $html .= getTree($data, $v['id'], $level + 1);
            $html = $html . "</li>\r\n";
        }
    }
    return $html ? '<ul>' . $html . '</ul>' : $html;
}
/**
 * 格式化字节大小
 * @param  number $size      字节数
 * @param  string $delimiter 数字和单位分隔符
 * @return string            格式化后的带单位的大小
 */
function format_bytes($size, $delimiter = '')
{
    $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
    for ($i = 0; $size >= 1024 && $i < 5; $i++) {
        $size /= 1024;
    }

    return round($size, 2) . $delimiter . $units[$i];
}

/**
 * 管理员操作日志
 * @param  [type] $data [description]
 * @return [type]       [description]
 */
function addlog($operation_id = '', $str = '')
{
    // //获取网站配置
    // $web_config = config('webconfig.is_log');
    // if ($web_config == 1) {
    //     $data['operation_id'] = $operation_id; //操作id
    //     $data['user_id']      = session('userid'); //管理员id
    //     $data['ip']           = \Request::ip(); //操作ip
    //     $data['create_time']  = time(); //操作时间
    //     $url['module']        = \Request::module();
    //     $url['controller']    = \Request::controller();
    //     $url['function']      = \Request::action();

    //     $strurl = $url['controller'] . '/' . $url['function'];

    //     if ($url['controller'] != 'Login') {
    //         //登录退出时不
    //         $data['menu_id'] = \Db::name('auth_rule')->where([['module', '=', $url['module']], ['name', '=', $strurl]])->value('id');
    //     } else {
    //         $data['menu_id'] = 1;
    //     }

    //     $data['menu_id'] = $data['menu_id'] ? $data['menu_id'] : 1;

    //     $data['url']  = $url['controller'] . '/' . $url['function'];
    //     $data['text'] = $str;

    //     \Db::name('admin_log')->insert($data);
    // } else {
    //     //关闭了日志
    //     return true;
    // }
 return true;
}

/**
 * 生成配制处表单
 * @param  [type] $fied 表单字段名
 * @param  [type] $val  表单值
 * @param  [type] $type 表单类型
 * @return [type]       [description]
 */
function createform($fied, $val, $type, $typeval)
{
    $html = '';
    if ($type == 1) {
        $html = '<textarea class="layui-textarea" name="' . $fied . '" lay-verify="content">' . $val . '</textarea>';
    } elseif ($type == 2) {
        //下拉
        $html .= '<select name="' . $fied . '" lay-verify="required" class="type-name" lay-filter="test">';
        if (!empty($typeval)) {
            $tes = array_unique(explode(',', $typeval));
            $tes = array_filter($tes);
            foreach ($tes as $k => $v) {
                $vs      = explode('|', $v);
                $vs[1]   = (isset($vs[1])) ? $vs[1] : $vs[0];
                $checked = ($val == $vs[0]) ? 'checked=""' : '';
                $html .= '<option value="' . $vs[0] . '">' . $vs[1] . ' ' . $checked . ' </option>';
            }
        }
        $html .= '</select>';
    } elseif ($type == 3) {
        //开关
        if (!empty($typeval)) {
            $tes = array_unique(explode(',', $typeval));
            $tes = array_filter($tes);
            foreach ($tes as $k => $v) {
                $vs      = explode('|', $v);
                $vs[1]   = (isset($vs[1])) ? $vs[1] : $vs[0];
                $checked = ($val == $vs[0]) ? 'checked=""' : '';
                $html .= '<input type="radio" name="' . $fied . '" value="' . $vs[0] . '" title="' . $vs[1] . '" ' . $checked . '>';

            }

        }
    } elseif ($type == 4) {
        //多选
        if (!empty($typeval)) {
            $tes = array_unique(explode(',', $typeval));
            $tes = array_filter($tes);
            foreach ($tes as $k => $v) {
                $vs      = explode('|', $v);
                $vs[1]   = (isset($vs[1])) ? $vs[1] : $vs[0];
                $checked = ($val == $vs[0]) ? 'checked=""' : '';
                $html .= '<input type="checkbox" name="' . $fied . '" lay-skin="primary" title="' . $vs[1] . '" checked="" value="' . $val . '" ' . $checked . '>';

            }

        }

    } else {
        $html = '<input type="text" name="' . $fied . '" lay-verify="title" autocomplete="off" class="layui-input" value="' . $val . '">';
    }
    return $html;
}

function ahuthbtn($act = '')
{
    if (empty($act)) {
        return false;
    }
    $auth = new \my\Auth();
    if (!$auth->check(\Request::controller() . '/' . $act, UID) && UID != '1') {
        return false;
    }
    return true;

}
